﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Enterprise;
using Traceability.App.Domain.Traceability;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructrue.Interface;

namespace Traceability.App.API.Application.Handler.Enterprise
{
    public class CreateSJGEnterpriseHandler : IRequestHandler<CreateSJGEnterpriseCommand, APIResult<int>>
    {
        private readonly IBaseRepository<EnterpriseModel> enterpriseRepo;
        private readonly IMapper mapper;

        public CreateSJGEnterpriseHandler(IBaseRepository<EnterpriseModel> enterpriseRepo,
                                        IMapper mapper)
        {
            this.enterpriseRepo = enterpriseRepo;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(CreateSJGEnterpriseCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var model = mapper.Map<EnterpriseModel>(request);
            int r = enterpriseRepo.AddAsync(model).Result;
            if (r > 0)
            {
                result.Message = "设置深加工企业基本信息成功";
                result.Data = r;
                result.Code = APIEnum.Ok;
            }
            else
            {
                result.Message = "设置深加工企业基本信息失败";
                result.Data = r;
                result.Code = APIEnum.Fail;
            }
            return Task.FromResult(result);
        }
    }
}
